//
//  LYLTimeLineVC.m
//  TimeLine
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 LYL. All rights reserved.
//

#import "LYLTimeLineVC.h"
#import "LYLTimeLineCell.h"
#import "LYLTimeLineCell_Empty.h"
#import "LYLTimeLineBlurView.h"
#import "LYLTimeLineBlurView_Empty.h"
#import "GHDiaryDataHandle.h"
#import "AppDelegate.h"
#import "GHDiaryDataModel.h"
#import "WYTTimeTool.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kSpace 10
#define kHeaderOrigineY kScreenHeight*0.151
#define kTimeLineCellSizeH kScreenHeight*0.27

#define kBaseImgOriginX 0.205*kScreenWidth
#define kBaseImgSizeW 0.775*kScreenWidth
#define kBaseImgSizeH 0.254*kScreenHeight

@interface LYLTimeLineVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *timeLineTV;

@property (nonatomic, strong) LYLTimeLineBlurView_Empty *blurView_empty;
@property (nonatomic, strong) LYLTimeLineBlurView *blurView;
// 表头视图
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIImageView *lineTopImg;
@property (nonatomic, strong) UILabel *topLabel;
// 数据数组
@property (nonatomic, strong) NSArray *diaryArray;

@property (nonatomic, strong) NSManagedObjectContext *manageContext;
@property (nonatomic, strong) GHDiaryDataHandle *handle;

@end

@implementation LYLTimeLineVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
//    GHDiaryDataHandle *handle = [[GHDiaryDataHandle alloc] init];
    NSArray *tempArr = [self.handle getModelArrayWithManagedObjectContext:self.manageContext];
    if (tempArr.count > 1) {
        self.diaryArray = [[tempArr reverseObjectEnumerator]allObjects];
    } else {
        self.diaryArray = tempArr;
    }
    [self.timeLineTV reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.backBtn.transform = CGAffineTransformMakeRotation(M_PI_2);
    }];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [UIView animateWithDuration:0.5 animations:^{
        self.backBtn.transform = CGAffineTransformMakeRotation(M_PI_2 * 3);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSubViews];
}

- (void)addSubViews
{
    UIImageView *baseImg = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    baseImg.image = [UIImage imageNamed:@"baseGroundFloor"];
    [self.view addSubview:baseImg];
    
    self.timeLineTV = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeaderOrigineY, kScreenWidth, kScreenHeight - kHeaderOrigineY) style:UITableViewStylePlain];
    self.timeLineTV.delegate = self;
    self.timeLineTV.dataSource = self;
    self.timeLineTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.timeLineTV.backgroundColor = [UIColor clearColor];
    self.timeLineTV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.timeLineTV];
    
// headerView
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderOrigineY)];
    [self.view addSubview:self.headerView];
    
// 返回按钮
    self.backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backBtn.frame = CGRectMake(15, (0.151*kScreenHeight - 40)/2, 40, 40);
    [self.backBtn setBackgroundImage:[UIImage imageNamed:@"timeline_gobtn"] forState:0];
    [self.backBtn addTarget:self action:@selector(chickBack) forControlEvents:1<<6];
    [self.headerView addSubview:self.backBtn];
// 白线顶端的图片
    self.lineTopImg = [[UIImageView alloc] initWithFrame:CGRectMake((0.175*kScreenWidth - 10 + 1), 0.141*kScreenHeight - 20, 20, 20)];
    self.lineTopImg.image = [UIImage imageNamed:@"timeline_top"];
    [self.headerView addSubview:self.lineTopImg];
// 多出的白线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0.175*kScreenWidth, 0.141*kScreenHeight, 2, 0.01*kScreenHeight)];
    self.lineView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:self.lineView];
// 文字label
    self.topLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 151 , (0.151*kScreenHeight - 60)/2, 131, 60)];
    self.topLabel.text = @"我 的 时 光 轴";
    self.topLabel.font = [UIFont systemFontOfSize:20];
    self.topLabel.textColor = [UIColor whiteColor];
    self.topLabel.textAlignment = NSTextAlignmentRight;
    [self.headerView addSubview:self.topLabel];
    
    self.blurView = [[LYLTimeLineBlurView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.blurView.frame = self.view.frame;
    self.blurView.alpha = 0.0f;
    [self.view addSubview:self.blurView];
    
    self.blurView_empty = [[LYLTimeLineBlurView_Empty alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.blurView_empty.frame = self.view.frame;
    self.blurView_empty.alpha = 0.0f;
    [self.view addSubview:self.blurView_empty];
}

- (void)chickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kTimeLineCellSizeH;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.diaryArray.count > 0) {
        return self.diaryArray.count;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.diaryArray.count > 0) {
        // 处理时间
        GHDiaryDataModel *model = self.diaryArray[indexPath.row];
        NSString *hourStr = [[model.dateLabelText substringFromIndex:11] substringToIndex:5];
        NSString *dateStr = [[model.dateLabelText substringFromIndex:0] substringToIndex:16];
        NSString *dateText = [WYTTimeTool NSTimerAboutTime:dateStr];
        NSString *IdStr = [[model.diaryID substringFromIndex:0] substringToIndex:6];
        if ([IdStr isEqualToString:@"186010"]) {
            static NSString *identifier_empty = @"TimeLineCellEmpty";
            LYLTimeLineCell_Empty *cell = [tableView dequeueReusableCellWithIdentifier:identifier_empty];
            if (cell == nil) {
                cell = [[LYLTimeLineCell_Empty alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier_empty];
            }
            cell.dateLabel.text = [NSString stringWithFormat:@"%@", dateText];
            cell.titleLabel.text = [NSString stringWithFormat:@"%@ |  %@", hourStr, model.summary];
            cell.baseImg.image = [UIImage imageNamed:@"timeline_paopao"];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            static NSString *identifier = @"TimeLineCell";
            LYLTimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[LYLTimeLineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            }
            cell.dateLabel.text = [NSString stringWithFormat:@"%@", dateText];
            cell.titleLabel.text = [NSString stringWithFormat:@"%@ |  %@", hourStr, model.title];
            cell.diaryImg.image = model.photoImg;
            cell.baseImg.image = [UIImage imageNamed:@"timeline_paopao"];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    } else {
        static NSString *identifier = @"TimeLineCell";
        LYLTimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[LYLTimeLineCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        cell.baseImg.image = [UIImage imageNamed:@"timeline_paopao"];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.diaryArray.count > 0) {
        GHDiaryDataModel *model = self.diaryArray[indexPath.row];
        NSString *IdStr = [[model.diaryID substringFromIndex:0] substringToIndex:6];
        if ([IdStr isEqualToString:@"186010"]) {
            [UIView animateWithDuration:0.3 animations:^{
                self.blurView_empty.alpha = 1.0f;
                self.blurView_empty.bodyText.text = model.summary;
                self.blurView_empty.dateLabel.text = model.dateLabelText;
            }];
        } else if ([IdStr isEqualToString:@"186011"]){
            [UIView animateWithDuration:0.3 animations:^{
                self.blurView.pictureImg.image = model.photoImg;
                self.blurView.alpha = 1.0f;
                self.blurView.bodyText.text = model.subtitle;
                self.blurView.locationLabel.text = model.placeLabel;
                self.blurView.dateLabel.text = model.dateLabelText;
            }];
        } else if ([IdStr isEqualToString:@"186012"] || [IdStr isEqualToString:@"186014"] || [IdStr isEqualToString:@"186015"]){
            [UIView animateWithDuration:0.3 animations:^{
                self.blurView.pictureImg.image = model.photoImg;
                self.blurView.alpha = 1.0f;
                self.blurView.bodyText.text = model.summary;
                self.blurView.locationLabel.text = model.placeLabel;
                self.blurView.dateLabel.text = model.dateLabelText;
            }];
        } else {
            [UIView animateWithDuration:0.3 animations:^{
                self.blurView.pictureImg.image = model.photoImg;
                self.blurView.alpha = 1.0f;
                self.blurView.bodyText.text = nil;
                self.blurView.locationLabel.text = model.placeLabel;
                self.blurView.dateLabel.text = model.dateLabelText;
            }];
        }
    } else {
        
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 开始
    NSArray *tempArr = [self.handle getModelArrayWithManagedObjectContext:self.manageContext];
    if (tempArr.count > 1) {
        self.diaryArray = [[tempArr reverseObjectEnumerator]allObjects];
    } else {
        self.diaryArray = tempArr;
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.timeLineTV reloadData];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 结束
}

// 表头的形变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < 0) {
        self.headerView.frame = CGRectMake(0, 0, kScreenWidth, kHeaderOrigineY - scrollView.contentOffset.y);
        self.lineTopImg.frame = CGRectMake((0.175*kScreenWidth - 10 + 1), 0.141*kScreenHeight - 20 - scrollView.contentOffset.y*0.7, 20, 20);
        self.lineView.frame = CGRectMake(0.175*kScreenWidth, 0.141*kScreenHeight - scrollView.contentOffset.y*0.7, 2, 0.01*kScreenHeight - scrollView.contentOffset.y*0.3);
        self.topLabel.frame = CGRectMake(kScreenWidth - 151 + scrollView.contentOffset.y*0.25, (0.151*kScreenHeight - 60)/2 - scrollView.contentOffset.y*0.25, 131 - scrollView.contentOffset.y*0.25, 60- scrollView.contentOffset.y*0.5);
        self.topLabel.font = [UIFont systemFontOfSize:20 - scrollView.contentOffset.y*0.04];
    }
}

- (GHDiaryDataHandle *)handle
{
    if (!_handle) {
        _handle = [[GHDiaryDataHandle alloc] init];
    }
    return _handle;
}

- (NSManagedObjectContext *)manageContext
{
    if (!_manageContext) {
        _manageContext= ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
    }
    return _manageContext;
}

- (NSArray *)diaryArray
{
    if (!_diaryArray) {
        _diaryArray = [NSArray array];
    }
    return _diaryArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
